/**
 * @Author 韦建韩
 * @Date 2022/5/16
 */
package com.example.devops.Manage;

import com.example.devops.web.controller.applicationAdmin.ProjectPublishProcessServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class ProjectPublishManage {
    Logger logger = LoggerFactory.getLogger(ProjectPublishManage.class);
    private String userId;
    private String gitAddr;
    private String branch;
    private String port;
    private String mirrorId;

    public ProjectPublishManage(String userId, String gitAddr, String branch, String port, String mirrorId) {
        this.userId = userId;
        this.branch = branch;
        this.gitAddr = gitAddr;
        this.port = port;
        this.mirrorId = mirrorId;
    }

   synchronized public void publish() throws IOException, InterruptedException {
        logger.info("inter script function");
       Process  p = Runtime.getRuntime().exec("/java/shell/projectPublish.sh" + " "+userId+" "+gitAddr+" "+branch+" "+port);
       BufferedReader bufferedReader = new BufferedReader(
               new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
       String line;
//       StringBuffer infoObject = ProcessInfoManage.getInfoObject();
       while ((line = bufferedReader.readLine()) != null) {
//           infoObject.append(line);
           logger.info(line);
           ProjectPublishProcessServer.sendInfo(line, mirrorId);
       }
       p.waitFor();
    }


}
